<?php
namespace app\common\model;

class Advert extends \think\Model
{
    private $table_name = 'hn_dvert';
    /** 封装一个广告对象 */
    public function fillAdvert($id, $cover, $title, $kind, $sort, $show)
    {
        $model = ['id' => $id, 'cover' => $cover, 'title' => $title, 'kind' => $kind, 'sort' => $sort, 'show' => $show];
        return $model;
    }
    /** 获取一个广告 */
    public function getAdvertInfo($id)
    {
        $model = \think\Db::table($this->table_name)->where('id', $id)->find();
        return $model;
    }
    /** 获取广告列表 */
    public function getAdvertList($kind, $show)
    {
        $query = \think\Db::table($this->table_name);

        if (strlen($kind) > 0) {
            $query = $query->where('kind', $kind);
        }
        if (strlen($show) > 0) {
            $query = $query->where('show', $show);
        }

        $lst = $query->select();
        return $lst;
    }
    /** 创建一个广告 */
    public function createAdvert($model)
    {
        if (strlen($model['sort']) < 1) {
            $model['sort'] = 0;
        }
        if (strlen($model['show']) < 1) {
            $model['show'] = 0;
        }

        $rule = [
            'cover' => 'require|max:50',
            'title' => 'max:50',
            'kind' => 'require|number',
            'sort' => 'require|number',
            'show' => 'require|number'
        ];
        $message = [
            'cover.require' => '请上传封面图',
            'cover.max' => '封面图字段超长',
            'title.max' => '广告标语长度为 1-50 个字符',
            'kind.require' => '请选择广告类型',
            'kind.number' => '广告类型只能是数字',
            'sort.number' => '排序是只能是数字',
            'show.number' => '显示状态只能是数字'
        ];

        $validate = new \think\Validate($rule, $message);

        if ($validate->check($model)) {
            $model['id'] = \uniqid();
            $res = \think\Db::table($this->table_name)->insert($model);

            if ($res > 0) {
                return ['flag' => \TRUE, 'data' => $model['id']];
            }
            else {
                return ['flage' => \FALSE, 'msg' => '数据库操作失败!'];
            }
        }
        else {
            return ['flag' => \FALSE, 'msg' => $validate->getError()];
        }
    }
    /** 修改广告信息 */
    public function updateAdvert($id, $newmodel)
    {
        $model = $this->getAdvertInfo($id);
        if (is_null($model)) {
            return ['flag' => \FALSE, 'msg' => '没有此广告'];
        }

        if ($model['cover'] != $newmodel['cover'] && strlen($newmodel['cover']) > 1) {
            $model['cover'] = $newmodel['cover'];
        }
        if ($model['title'] != $newmodel['title'] && strlen($newmodel['title']) > 1) {
            $model['title'] = $newmodel['title'];
        }
        if ($model['kind'] != $newmodel['kind'] && strlen($newmodel['kind']) > 1) {
            $model['kind'] = $newmodel['kind'];
        }
        if ($model['sort'] != $newmodel['sort'] && strlen($newmodel['sort']) > 1) {
            $model['sort'] = $newmodel['sort'];
        }
        if ($model['show'] != $newmodel['show'] && strlen($newmodel['show']) > 1) {
            $model['show'] = $newmodel['show'];
        }

        $rule = [
            'cover' => 'require|max:50',
            'title' => 'max:50',
            'kind' => 'require|number',
            'sort' => 'require|number',
            'show' => 'require|number'
        ];
        $message = [
            'cover.require' => '请上传封面图',
            'cover.max' => '封面图字段超长',
            'title.max' => '广告标语长度为 1-50 个字符',
            'kind.require' => '请选择广告类型',
            'kind.number' => '广告类型只能是数字',
            'sort.number' => '排序是只能是数字',
            'show.number' => '显示状态只能是数字'
        ];

        $validate = new \think\Validate($rule, $message);

        if ($validate->check($model)) {
            $res = \think\Db::table($this->table_name)->where('id', $id)->update($model);

            if ($res > 0) {
                return ['flag' => \TRUE, 'data' => $model['id']];
            }
            else {
                return ['flage' => \FALSE, 'msg' => '数据库操作失败!'];
            }
        }
        else {
            return ['flag' => \FALSE, 'msg' => $validate->getError()];
        }
    }
    /** 溢出一个广告 */
    public function removeAdvert($id)
    {
        $res = \think\Db::table($this->table_name)->where('id', $id)->update(['show' => 0]);
        if ($res > 0) {
            return ['flag' => \TRUE, 'data' => $id];
        }
        else {
            return ['flag' => \FALSE, 'msg' => '操作数据库失败!'];
        }
    }
}